use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

mod db;
mod meta;

#[proc_macro_derive(Db, attributes(db))]
pub fn db_derive(input: TokenStream) -> TokenStream {
    let st = parse_macro_input!(input as DeriveInput);

    let name = meta::ident(&st);
    let dm = db::parse_db_meta(&st);

    // println!("{:#?}", dm);

    let insert_ts = db::insert_ts(&dm);
    let update_ts = db::update_ts(&dm);
    let find_by_ts = db::find_by_ts(&dm);
    let find_ts = db::find_ts(&dm);
    let list_filter_ts = db::list_filter_ts(&dm);
    let list_ts = db::list_ts(&dm);
    let del_ts = db::del_ts(&dm);
    let self_update_ts = db::self_update_ts(&dm);

    quote! {
        impl #name {
            #insert_ts

            #update_ts

            #find_ts

            #list_ts

            #del_ts

            #self_update_ts

        }

        #find_by_ts

        #list_filter_ts
    }
    .into()
}
